//
//  XLGResult.m
//  etionCBLogic
//
//  Created by wjxfhxy on 15-4-17.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGResult.h"

@interface XLGResult ()

@property(nonatomic,assign) NSInteger outResult;
@property(nonatomic,retain) id outObj;
@property(nonatomic,retain) NSError *outError;

@end

@implementation XLGResult

@synthesize outResult = _outResult;
@synthesize outObj = _outObj;
@synthesize outError = _outError;

- (id)initWithOutResult:(NSInteger)outResult outObj:(id)outObj outError:(NSError*)outError
{
    self = [super init];
    
    _outResult = outResult;
    _outObj = [outObj retain_mm];
    _outError = [outError retain_mm];
    
    return self;
}

- (void)dealloc
{
    [_outError release_mm];
    [_outObj release_mm];
    superdealloc_mm;
}

- (NSString*)debugDescription
{
    return [NSString stringWithFormat:@"%@,%@,%@",@(_outResult),_outObj,_outError];
}

+ (XLGResult*)result
{
    XLGResult* result = [[[XLGResult alloc] initWithOutResult:FALSE outObj:nil outError:nil] autorelease_mm];
    
    return result;
}

+ (XLGResult*)resultWithOutResult:(NSInteger)outResult
{
    XLGResult* result = [[[XLGResult alloc] initWithOutResult:outResult outObj:nil outError:nil] autorelease_mm];
    
    return result;
}

+ (XLGResult*)resultWithOutResult:(NSInteger)outResult outObj:(id)outObj
{
    XLGResult* result = [[[XLGResult alloc] initWithOutResult:outResult outObj:outObj outError:nil] autorelease_mm];
    
    return result;
}

+ (XLGResult*)resultWithOutResult:(NSInteger)outResult outObj:(id)outObj outError:(NSError*)outError
{
    XLGResult* result = [[[XLGResult alloc] initWithOutResult:outResult outObj:outObj outError:outError] autorelease_mm];
    
    return result;
}

@end


@implementation XLGResult (OutObj)

- (NSString*)outObjString
{
    BOOL b = [self.outObj isKindOfClass:[NSString class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a string");
    }
#endif
    
    return b == YES ? self.outObj : nil;
}

- (NSArray*)outObjArray
{
    BOOL b = [self.outObj isKindOfClass:[NSArray class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a array");
    }
#endif
    
    return b == YES ? self.outObj : nil;
}

- (NSDictionary*)outObjDictionary
{
    BOOL b = [self.outObj isKindOfClass:[NSDictionary class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a dictionary");
    }
#endif
    
    return b == YES ? self.outObj : nil;
}

- (NSNumber*)outObjNumber
{
    BOOL b = [self.outObj isKindOfClass:[NSNumber class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a number");
    }
#endif
    
    return b == YES ? self.outObj : nil;
}

- (BOOL)outObjBool
{
    BOOL b = [self.outObj isKindOfClass:[NSNumber class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a number");
    }
#endif
    
    return b == YES ? [self.outObj boolValue] : NO;
}

- (NSInteger)outObjInt
{
    BOOL b = [self.outObj isKindOfClass:[NSNumber class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj not a number");
    }
#endif
    
    return b == YES ? [self.outObj integerValue] : NSNotFound;
}

- (NSUInteger)outObjUInt
{
    BOOL b = [self.outObj isKindOfClass:[NSNumber class]] ? YES : NO;
#ifdef DEBUG
    if(self.outObj != nil)
    {
        NSAssert(b, @"outObj is not a number");
    }
#endif
    
    return b == YES ? [self.outObj unsignedIntegerValue] : NSNotFound;
}

@end
